from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn import metrics, svm
import pandas as pd
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# 在服务上跑的数据是20201226日生成的data
# 服务器的path的内容
# path = "rawdata2020122602.csv"

path = "D:/learn/school/code/myfinalpaper/data/rawdata2020122602.csv"

df = pd.read_csv(path)
x = df['data']
y = df['label']


def do_metrics(y_test_truth, y_test_pred):
    print("metrics.accuracy_score:")
    print(metrics.accuracy_score(y_test_truth, y_test_pred))
    print("metrics.confusion_matrix:")
    print(metrics.confusion_matrix(y_test_truth, y_test_pred))
    print("metrics.precision_score:")
    print(metrics.precision_score(y_test_truth, y_test_pred))
    print("metrics.recall_score:")
    print(metrics.recall_score(y_test_truth, y_test_pred))
    print("metrics.f1_score:")
    print(metrics.f1_score(y_test_truth, y_test_pred))
    TN = metrics.confusion_matrix(y_test_truth, y_test_pred)[0, 0]
    FP = metrics.confusion_matrix(y_test_truth, y_test_pred)[0, 1]
    FN = metrics.confusion_matrix(y_test_truth, y_test_pred)[1, 0]
    TP = metrics.confusion_matrix(y_test_truth, y_test_pred)[1, 1]
    print("TN: " + str(TN))
    print("FP: " + str(FP))
    print("FN: " + str(FN))
    print("TP: " + str(TP))
    print("真正率TPR: " + str(TP/(TP+FN)))
    print("假正率FPR漏报率: " + str(FP/(FP+TN)))
    print("假负率FNR误报率: " + str(FN/(TP+FN)))
    print("真负率TNR: " + str(TN/(TN+FP)))



def bag_mlp(x, y, grame):
    # os.mkdir("bag_mlp" + str(grame))
    cv = CountVectorizer(ngram_range=(grame, grame),
                         max_features=10000)
    x = cv.fit_transform(x).toarray()
    # 转换为tfidf
    # x = TfidfTransformer().fit_transform(x).toarray()

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
    clf = MLPClassifier(solver='lbfgs',
                        alpha=1e-5,
                        hidden_layer_sizes=(5, 2),
                        random_state=1)
    clf.fit(x_train, y_train)
    y_pred = clf.predict(x_test)
    print("*****************************************")
    print("bag_mlp" + str(grame) + "模型预测")
    do_metrics(y_test, y_pred)
    print("预测结束")
    print("*****************************************")
    dataframe = pd.DataFrame({'y_test_truth': y_test,
                              'y_predict_score': clf.predict_proba(x_test)[:, 1],
                              'y_predict_label': y_pred})
    dataframe.to_csv("./bag_mlp/bag_mlp" + str(grame) + '.csv', sep=',', index=False)

if __name__ == "__main__":
    os.mkdir("bag_mlp")
    bag_mlp(x, y, 1)
    bag_mlp(x, y, 2)
    bag_mlp(x, y, 3)
    bag_mlp(x, y, 4)
    bag_mlp(x, y, 5)